package de.unibielefeld.msp.zusatzstoffe;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

/**
 * Einstellungs-Activity.
 * 
 * @author Sabine
 * 
 */


public class Settings extends Activity {
	
	SQLHelper allData;
	Cursor c;
	ListAdapter adapter;
	int selected;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        setContentView(R.layout.settings);

		Button submit = (Button)findViewById(R.id.submit);
		
		allData = new SQLHelper(this);
		final ListView list = (ListView) findViewById(R.id.settingsList);
		
		submit.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				EditText name = (EditText)findViewById(R.id.settingsInput);
				addData(name.getText().toString());
				name.setText("");
				c.requery();
				 
			}
		});
		
		final AlertDialog.Builder builder = new AlertDialog.Builder(this);
		final AlertDialog.Builder removebuilder = new AlertDialog.Builder(this); 
		
		c = getData();
		startManagingCursor(c);
		adapter = new SimpleCursorAdapter(this, android.R.layout.two_line_list_item,
				c, new String[] {"name", "grade"},
				new int[] {android.R.id.text1, android.R.id.text2});
		list.setAdapter(adapter);
		
		
		list.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
				final CharSequence[] items = {"hochallergen", "allergen"};
				builder.setTitle("Wähle den Allergiegrad");
				selected = -1;
				final String name = ((TextView)((LinearLayout)arg1).getChildAt(0)).getText().toString();
				builder.setSingleChoiceItems(items, selected, new DialogInterface.OnClickListener() {
				    public void onClick(DialogInterface dialog, int item) {
				        selected = item;
				    }
				});
				builder.setPositiveButton("Übernehmen", new DialogInterface.OnClickListener() {  
                    public void onClick(DialogInterface dialog, int which) {  
                        updateData(name , items[selected].toString());
                        c.requery();
                        return;
                } });
				final AlertDialog alert = builder.create();
                alert.show();
			}
		});
		
		list.setOnItemLongClickListener(new OnItemLongClickListener() {
		
		    public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
		        final String name = ((TextView)((LinearLayout)arg1).getChildAt(0)).getText().toString();
		        removebuilder.setTitle("Soll die Allergie gelöscht werden?");  
		        removebuilder.setPositiveButton("Ja", new DialogInterface.OnClickListener() {  
		            public void onClick(DialogInterface dialog, int which) {  
		                removeData(name);
		                c.requery();
		                return;
		            } });   
		        removebuilder.setNegativeButton("Nein", new DialogInterface.OnClickListener() {  
		            public void onClick(DialogInterface dialog, int which) {  
		                return;  
		            }}); 
		        final AlertDialog alert = removebuilder.create();
                alert.show();
                return true;
		    }
	
		});
	}
	
	private void addData(String title) {
	    SQLiteDatabase db = allData.getWritableDatabase();
	    ContentValues values = new ContentValues();
	    values.put(SQLHelper.NAME, title);
	    values.put(SQLHelper.GRADE, "hochallergen");
	    db.insert(SQLHelper.TABLE, null, values);
	}

	
	private Cursor getData() {
	    SQLiteDatabase db = allData.getReadableDatabase();
	    Cursor cursor = db.query(SQLHelper.TABLE, null, null, null, null,
	        null, null);
	    
	    startManagingCursor(cursor);
	    return cursor;
	}
	
	private void updateData(String title, String allergy) {
		SQLiteDatabase db = allData.getWritableDatabase();
	    ContentValues values = new ContentValues();
	    values.put(SQLHelper.NAME, title);
	    values.put(SQLHelper.GRADE, allergy);
	    db.update(SQLHelper.TABLE, values, SQLHelper.NAME+"=?",new String[]{title});
	    c.requery();
	}
	
	private void removeData(String title){
	    SQLiteDatabase db = allData.getWritableDatabase();
	    db.delete(SQLHelper.TABLE, SQLHelper.NAME+"=?", new String[]{title});
	}
}